home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / disk / misc / ADFlib.lha / Lib / Win32 / nt4_dev.c < prev    next >
C/C++ Source or Header  |  1999-02-08  |  3KB  |  122 lines

  1. /* nt4_dev.c - routines for direct drive access in Windows NT 4.0
  2.  *
  3.  * Copyright 1999 by Dan Sutherland <dan@chromerhino.demon.co.uk>
  4.  *
  5.  * These routines only currently work with drives <2GB and 512 bytes per sector
  6.  */
  7.  
  8. #include <windows.h>
  9. #include <winioctl.h>
  10. #include <stdio.h>
  11. #include "nt4_dev.h"
  12.  
  13. HANDLE NT4OpenDrive(char *lpstrDrive)
  14. {
  15.     char strDriveFile[40];
  16.     HANDLE hDrv;
  17.     DWORD dwRet;
  18.  
  19.     switch (lpstrDrive[0]) {
  20.         case 'H':
  21.             sprintf(strDriveFile, "\\\\.\\PhysicalDrive%c", lpstrDrive[1]);
  22.             break;
  23.         /* add support for other device types here */
  24.         default:
  25.             return NULL;
  26.     }
  27.  
  28.     hDrv = CreateFile(strDriveFile, GENERIC_READ | GENERIC_WRITE,
  29.         FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
  30.         0, NULL);
  31.  
  32.     if (hDrv == INVALID_HANDLE_VALUE)
  33.         return NULL;
  34.  
  35.     if (! DeviceIoControl(hDrv, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0,
  36.         &dwRet, NULL))
  37.         return NULL;
  38.  
  39.     return hDrv;
  40. }
  41.  
  42. BOOL NT4CloseDrive(HANDLE hDrv)
  43. {
  44.     DWORD dwRet;
  45.  
  46.     if (! DeviceIoControl(hDrv, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0,
  47.         &dwRet,    NULL))
  48.         return FALSE;
  49.  
  50.     if (! CloseHandle(hDrv))
  51.         return FALSE;
  52.  
  53.     return TRUE;
  54. }
  55.  
  56. BOOL NT4ReadSector(HANDLE hDrv, long iSect, int iSize, void *lpvoidBuf)
  57. {
  58.     void *lpvoidTempBuf;
  59.     DWORD dwActual;
  60.  
  61.     lpvoidTempBuf = VirtualAlloc(NULL, 512, MEM_COMMIT, PAGE_READWRITE);
  62.  
  63.     if (SetFilePointer(hDrv, iSect * 512, NULL, FILE_BEGIN) == 0xFFFFFFFF) {
  64.         VirtualFree(lpvoidTempBuf, 0, MEM_RELEASE);
  65.         return FALSE;
  66.     }
  67.  
  68.     if (! ReadFile(hDrv, lpvoidTempBuf, 512, &dwActual, NULL)) {
  69.         VirtualFree(lpvoidTempBuf, 0, MEM_RELEASE);
  70.         return FALSE;
  71.     }
  72.  
  73.     memcpy(lpvoidBuf, lpvoidTempBuf, iSize);
  74.     VirtualFree(lpvoidTempBuf, 0, MEM_RELEASE);
  75.  
  76.     return TRUE;
  77. }
  78.  
  79. BOOL NT4WriteSector(HANDLE hDrv, long iSect, int iSize, void *lpvoidBuf)
  80. {
  81.     void *lpvoidTempBuf;
  82.     DWORD dwActual;
  83.  
  84.     if (iSize != 512)
  85.         return FALSE;
  86.  
  87.     lpvoidTempBuf = VirtualAlloc(NULL, 512, MEM_COMMIT, PAGE_READWRITE);
  88.  
  89.     if (SetFilePointer(hDrv, iSect * 512, NULL, FILE_BEGIN) == 0xFFFFFFFF) {
  90.         VirtualFree(lpvoidTempBuf, 0, MEM_RELEASE);
  91.         return FALSE;
  92.     }
  93.  
  94.     memcpy(lpvoidTempBuf, lpvoidBuf, iSize);
  95.  
  96.     if (! WriteFile(hDrv, lpvoidTempBuf, 512, &dwActual, NULL)) {
  97.         VirtualFree(lpvoidTempBuf, 0, MEM_RELEASE);
  98.         return FALSE;
  99.     }
  100.     
  101.     VirtualFree(lpvoidTempBuf, 0, MEM_RELEASE);
  102.  
  103.     return TRUE;
  104. }
  105.  
  106. ULONG NT4GetDriveSize(HANDLE hDrv)
  107. {
  108.     DWORD dwActual;
  109.     DISK_GEOMETRY dgGeom;
  110.     long size;
  111.  
  112.     DeviceIoControl(hDrv, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0,
  113.         &dgGeom, sizeof(DISK_GEOMETRY), &dwActual, NULL);
  114.  
  115.     size =  dgGeom.Cylinders.LowPart * dgGeom.TracksPerCylinder *
  116.         dgGeom.SectorsPerTrack * dgGeom.BytesPerSector;
  117.  
  118.     printf("Total sectors: %i\n", dgGeom.Cylinders.LowPart * dgGeom.TracksPerCylinder * dgGeom.SectorsPerTrack);
  119.     printf("Byte size: %i\n", size);
  120.  
  121.     return size;
  122. }